from  appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Base():
    def __init__(self):
        #连接设备
        cap={
            "platformName": "Android",
            "deviceName": "emulator-5554",
            "automationName": "uiautomator2",
            "appPackage": "com.kongfz.app",
            "appActivity": ".home.mvvm.homepage.HomeActivity",
            "noReset": True
        }
        self.driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',cap)
        #隐式等待
        self.driver.implicitly_wait(10)

    #查找元素
    def findele(self,loc):
        ele=self.driver.find_element(*loc)
        return ele

    #显示等待找元素
    def findele_wait(self,loc):
        ele=WebDriverWait(self.driver,10).until(EC.element_to_be_clickable(loc))
        return  ele
    #滑动
    def hua(self,a,b,c,d):
        self.driver.swipe(a,b,c,d,200)
    #截图
    def jietu(self,a):
        self.driver.save_screenshot(a)
    #退出驱动
    def tuichu(self):
        self.driver.quit()
    #点击
    def dianji(self,a,b):
        self.driver.tap([(a,b)],200)
    #返回按钮
    def fanhui(self):
        self.driver.press_keycode(3)
    #长按
    def changan(self,loc):
        ele=self.driver.find_element(*loc)
        action=TouchAction(self.driver)
        action.long_press(el=ele,duration=1000).release().perform()
    #拖拽
    def tuozhuai(self,start,end):
        action = TouchAction(self.driver)
        action.press(x=start[0],y=start[1]).wait(ms=800).move_to(x=end[0],y=end[1]).release().perform()